package cn.webrx.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@Import(DruidConfig.class)
@ComponentScan("cn.webrx.mapper")
@MapperScan(basePackages = "cn.webrx.mapper",sqlSessionFactoryRef = "sf")
public class SpringConfig {
    @Bean("sf")
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource ds) throws IOException {
        var sf = new SqlSessionFactoryBean();
        sf.setTypeAliasesPackage("cn.webrx.entity");
        sf.setDataSource(ds);
        sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));
        return sf;
    }
}
